package com.handsoninfo;

import net.sourceforge.tess4j.Word;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MultiImageUi extends JPanel implements MouseMotionListener {
    private static class Region {
        BufferedImage bi;

        List<Word> words;

        Division divisions;

        int offsetX;
        int offsetY;
    }

    int cursorX = 0;
    int cursorY = 0;

    int windowWidth = 0;
    int windowHeight = 0;

    final DecimalFormat df = new DecimalFormat("#0.0");
    final List<Region> instances = new ArrayList<>();

    final JFrame frame = new JFrame();

    public void put(Recognition.Instance instance) {
        int x = cursorX + instance.bi.getWidth() > 1500 ? 0 : cursorX;
        int y = x == 0 ? windowHeight : cursorY;
        windowWidth = Math.max(x + instance.bi.getWidth(), windowWidth);
        windowHeight = Math.max(y + instance.bi.getHeight(), windowHeight);
        cursorX = x + instance.bi.getWidth();
        cursorY = y;
        instances.add(new Region() {{
            bi = instance.bi;
            words = instance.words;
            divisions = instance.divisions;
            offsetX = x;
            offsetY = y;
        }});
//        repaint();
        if (windowHeight < 800) {
            frame.setSize(windowWidth + 17, windowHeight + 40);
        }
        setPreferredSize(new Dimension(windowWidth, windowHeight));

    }

    public MultiImageUi() {
        addMouseMotionListener(this);
        // create a JFrame to display the image
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(this);
        frame.getContentPane().add(scrollPane);

//        frame.pack(); // 计算并设置 JFrame 大小
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
//        System.out.println(frame.getContentPane().getBounds());
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        Rectangle rect = frame.getContentPane().getBounds();
        frame.setTitle("(" + x + ", " + y + ")" + rect);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        for (Region region : instances) {
            g.drawImage(region.bi, region.offsetX, region.offsetY, null);
            g.setColor(Color.RED);
            Font font = new Font("Serif", Font.BOLD, 26);
            g.setFont(font);
            for (Word word : region.words) {
                Rectangle rect = word.getBoundingBox();
//            if ("莫".equals(word.getText())) g.fillRect(rect.x, rect.y, rect.width, rect.height);
                if (!region.divisions.containsWord(word)) continue;
                g.drawString(word.getText(), region.offsetX + rect.x, region.offsetY + rect.y);
            }
            for (Word word : region.words) {
                if (!region.divisions.containsWord(word)) continue;
                Rectangle rect = word.getBoundingBox();
                Color color = Utils.getTextColor(region.bi, word);
                assert(null != color); // `divisions.containsWord(word)`这个判断已经检查过
                g.setColor(color);
                g.drawRect(region.offsetX + rect.x, region.offsetY + rect.y, rect.width, rect.height);
            }
            g.setColor(Color.BLACK);
            font = new Font("Serif", Font.BOLD, 12);

            g.setFont(font);
            for (Word word : region.words) {
                if (!region.divisions.containsWord(word)) continue;
                Rectangle rect = word.getBoundingBox();
                g.drawString(df.format(word.getConfidence()) + "%", region.offsetX + rect.x, region.offsetY + rect.y);
            }

            g.setColor(Color.GREEN);
            for (Division.Block block : region.divisions.getBlocks()) {
                Rectangle rect = block.getRect();
                g.drawRect(region.offsetX + rect.x, region.offsetY + rect.y, rect.width, rect.height);
            }
        }

    }
}
